#include "..\include\imp_typedefine.h"
#include "..\include\imp_io_wrapper.h"
#include "..\include\imp_os_wrapper.h"
#include "..\include\imp_pictext_wrapper.h"

#include ".\adapter\mbm_font_adapter.h"
#include ".\adapter\mbm_sys_adapter.h"
#include ".\adapter\mbm_vui_adapter.h"


static UInt8 * utf8_2_ucs2_char(UInt8 *utf8DataP, UInt16 *ucs2)
{
    UInt16 data = 0;
    Int32 offset = 0;

    if ((utf8DataP[0] & 0xE0) == 0xE0)
    {
        data |= (utf8DataP[0] & 0x0F) << 12;
        data |= (utf8DataP[1] & 0x3F) << 6;
        data |= (utf8DataP[2] & 0x3F);
        offset = 3;
    }
    else if ((utf8DataP[0] & 0xC0) == 0xC0)
    {
         data |= (utf8DataP[0] & 0x3F) << 6;
         data |=  utf8DataP[1] & 0x3F;
         offset = 2;
    }
    else
    {
        data = utf8DataP[0];
        offset = 1;
    }

    *ucs2 = data;
    return utf8DataP + offset;
}

e_imp_charlib_type impict_charlib_get_type(void)
{
    Int32 type = mbm_charlib_get_type();

    e_imp_charlib_type ftype;
    
    if (2 == type)
    {
        ftype = IMP_Charlib_vector;
    }
    else if (1 == type)
    {
        ftype = IMP_Charlib_bitmap;
    }
    else
    {
        IMP_Assert(0, "impict_charlib_get_type: not support type", type);
    }

    return ftype;
}

UInt8 *impict_char_size_vector(UInt8 *str, Int32 *cw, Int32 *ch)
{
    UInt8 *ret = 0;
	UInt16 ucs2 = 0;

    IMP_Assert(str != 0 && cw != 0 && ch != 0, "impict_char_size_vector(0)");

    ret = utf8_2_ucs2_char(str, &ucs2); 

	mbm_get_char_size(ucs2, cw, ch);
    
	return ret;
}

UInt8 *impict_draw_char_vector(UInt8 *str, Int8 *buffer, Int32 stride, UInt16 color16, IMP_Rect *disp_rect, IMP_Rect *clip_rect)
{
    MBM_Rect d, c;
    UInt8 *ret = 0;
	UInt16 ucs2 = 0;

    d.x = disp_rect->x;
    d.y = disp_rect->y;
    d.w = disp_rect->w;
    d.h = disp_rect->h;

    c.x = clip_rect->x;
    c.y = clip_rect->y;
    c.w = clip_rect->w;
    c.h = clip_rect->h;

    IMP_Trace("disp: %d-%d-%d-%d", d.x, d.y, d.w, d.h);
    IMP_Trace("clip: %d-%d-%d-%d", c.x, c.y, c.w, c.h);
    IMP_Trace("stride: %d, color: %x", stride, color16);
    ret = utf8_2_ucs2_char(str, &ucs2); 

	if(c.w>0 && c.h>0)
		mbm_draw_char_vector(ucs2, buffer, stride, color16, &d, &c);
    return ret;
}

UInt8 *impict_get_char_bitmap(UInt8 *str, UInt8 **word, Int32 *cw, Int32 *ch, Int32 *bit_stride)
{
    UInt8 *ret = 0;
	UInt16 ucs2 = 0;

    IMP_Assert(str != 0 && word != 0 && cw != 0 && ch != 0, 0);

    ret = utf8_2_ucs2_char(str, &ucs2); 

	mbm_get_char_bitmap(ucs2, word, cw, ch, bit_stride);
    
	return ret;
}

Int32 impict_get_font_height(void)
{
	return mbm_get_font_height();
}

Int32 impict_charlib_getbit(UInt8 *word, Int32 bit)
{  
	return mbm_get_char_bit(word, bit);
}

void impict_set_font(Int32 font)
{
	if(IMP_Font_tiny == font)
	{
		font = MBM_Font_tiny;
	}
	else if(IMP_Font_small == font)
	{
		font = MBM_Font_small;
	}
	else if(IMP_Font_medium == font)
	{
		font = MBM_Font_medium;
	}
	else if(IMP_Font_large == font)
	{
		font = MBM_Font_large;
	}
	else if(IMP_Font_huge == font)
	{
		font = MBM_Font_huge;
	}
	else
	{
		IMP_Assert(0, "impict_set_font unsupport font=%d!", font);
	}

	mbm_set_font_type(font);	
}

Int32 impict_get_screen_width()
{
	return mbm_get_screen_width();
}

Int32 impict_get_screen_height()
{
	return mbm_get_screen_height();
}

void impict_displ_draw(Int32 x, Int32 y, Int32 w, Int32 h)
{
	mbm_Display_DirectDrawImg(x, y, w, h, 0);
}

void impict_displ_show(Int32 x, Int32 y, Int32 w, Int32 h)
{
	mbm_Display_DirectDrawImg(x, y, w, h, 0);
}

Int32 impict_displ_rotation( Int32 angle )
{
	IMP_Assert(0, "impict_displ_rotation under construct!");
}


//////////////////////////////////
// return next position of input utf8DataP
UInt8 *imp_utf8_2_ucs2_char(UInt8 *utf8DataP, UInt16 *ucs2)
{
    UInt16 data = 0;
    Int32 offset = 0;

    if ((utf8DataP[0] & 0xE0) == 0xE0)
    {
        data |= (utf8DataP[0] & 0x0F) << 12;
        data |= (utf8DataP[1] & 0x3F) << 6;
        data |= (utf8DataP[2] & 0x3F);
        offset = 3;
    }
    else if ((utf8DataP[0] & 0xC0) == 0xC0)
    {
         data |= (utf8DataP[0] & 0x3F) << 6;
         data |=  utf8DataP[1] & 0x3F;
         offset = 2;
    }
    else
    {
        data = utf8DataP[0];
        offset = 1;
    }

    *ucs2 = data;
    return utf8DataP + offset;
}

